package com.doumi.biz.qadev.api.testcase.commonfilter;

import com.doumi.biz.qadev.api.methods.OperationRedis;
import com.doumi.biz.qadev.api.testcase.RequestTemplate;
import io.restassured.path.json.JsonPath;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YDZ on 2019/11/20.
 */
public class CommonFilterBehavior {
    private RequestTemplate requestTemplate = new RequestTemplate();
    private OperationRedis operationRedis = new OperationRedis();
    private static List<String> cuIds = new ArrayList<>();

    //redis初始化数据
    @BeforeClass(description = "初始化redis数据,数据中有两个用户，用户25896有商家行为，用户85694没有商家行为")
    public  void operationData(){
        String filenanme = "src/source/redisfile/filter-business_behavior.csv";
        String mothodname = "business_behavior";
        cuIds = operationRedis.readCsvoperationRedis(filenanme,mothodname,1);
    }
    @Test(description = "所有用户信息可在Configuration中查看,测试1个用户为正常信息，另一个用户为有商家行为用户，正常结果返回所有userid=85694；校验传入的结果中是否全部返回，code状态码是否为0，msg信息是否为success")
    public void checkHaveBusinessBehaviocRequest(){
        StringBuffer sb = new StringBuffer("[");
        for (int i = 0; i <cuIds.size()-1;  i++) {
            sb.append(cuIds.get(i).trim()).append(",");
        }
        sb.append(cuIds.get(cuIds.size()-1).trim());
        sb.append("]");
        HashMap<String,Object> hashMap = new HashMap();
        hashMap.put("cuIds",sb.toString());
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String msg = jsonPath.getString("msg");
        Assert.assertEquals(String.valueOf(resurlt.get(0)),String.valueOf("85694"));
        Assert.assertEquals(msg,"success");
    }

    @Test(description = "请求参数cuIds为一个用户，该用户为有商家行为的用户，该用户被过滤掉返回空")
    public void checkOnlyOneCuerIdsIsrefiltered(){
        HashMap hashMap = new HashMap();
        hashMap.put("cuIds","25896");
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt =new ArrayList<>();
        resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(resurlt.size(),0);
        Assert.assertEquals(code,"200");
    }
    @Test(description = "参数cuIds格式为[85694]时，该用户为正常用户信息，返回数据的size为1，code为0，返回的data中的信息为该userid本身")
    public void checkOnlyOneCuerIdsIsSaved(){
        HashMap hashMap = new HashMap();
        String cuIds="[85694]";
        hashMap.put("cuIds",cuIds);
        hashMap.put("businessType","1");
        JsonPath jsonPath =requestTemplate.postCommonFilter(hashMap);
        List resurlt = jsonPath.getList("data");
        String code = jsonPath.getString("code");
        Assert.assertEquals(String.valueOf(85694),String.valueOf(resurlt.get(0)));
        Assert.assertEquals(code,"200");
    }


    //删除数据
    @AfterClass(description = "删除出入掉的数据，避免影响其他测试")
    public void removeData(){
        operationRedis.removeRedis(cuIds);
    }
}
